// index.js
var api = require("../../api.js"),
  app = getApp();
var WxParse = require("../../wxParse/wxParse.js");
Page({
  data: {
    vipContent: [],
    content:'',
    showModalStatus: false, //是否显示
    type: [],
    currentIndex: 0,
    num: 1,
    price: 0,
    agree: false
  },

  /**
   * 生命周期函数--监听页面加载
   */ 
  onLoad: function(options) {
    console.log(options);
    var _that = this;
    var userGrade = wx.getStorageSync("userGrade")
    if(userGrade){
        _that.setData({
            userGrade:userGrade 
        })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    if (getApp().globalData.userInfo != null) {
      // 高进用户全益
      var t = new Object();
      t.userGrade = that.data.userGrade;
      wx.request({
        url: api.mine.equityContent,
        data: t,
        success: function(res) {
          console.log(res.data);
          if (res.statusCode == 200) {
            if (res.data.code == 200) {
              that.setData({
                content: res.data.data.noticeContent,
              })
              WxParse.wxParse(
                "vipDescription",
                "html",
                res.data.data.noticeContent,
                that,
                5
              );
            }
          }
        }
      });
      wx.request({
        url: api.mine.renewVIP,
        data: { openId:getApp().globalData.wxUserInfo.openId},
        success: function(res) {
          console.log(res.data);
          if (res.statusCode == 200) {
            if (res.data.code == 200) {
                that.setData({
                    price:res.data.data.vipspecPrice,
                    vipspecPrice:res.data.data.vipspecPrice
                })
            }
          }
        }
      });
    }
  },

  //点击购买
  buy:function() {
    var that = this;
    var t = new Object();
    t = {
      body: "会员续费",
      ordType: 5,
      openId:getApp().globalData.wxUserInfo.openId,
      years:that.data.num,
      ordMoney: that.data.vipspecPrice
    };
    wx.request({
      url: api.pay.toBuy,
      data: t,
      success: function(res) {
        console.log(res);
        if (res.statusCode == 200 && res.data.code == 200) {
          var data = res.data.data;
          console.log(data);
          wx.requestPayment({
            timeStamp: data.timeStamp.toString(),
            nonceStr: data.nonceStr,
            package: data.package,
            signType: "MD5",
            paySign: data.paySign,
            success: function(res) {
              console.log("支付成功");
            },
            fail: function(res) {
              console.log("支付失败");
              return;
            },
            complete: function(res) {
              console.log("支付完成");
              console.log(res);
              if (res.errMsg == "requestPayment:ok") {
                wx.showToast({
                  title: "续费成功",
                  icon: 'succes',
                  duration: 1000,
                });
                setTimeout(() => {
                 //上一个页面实例对象
                 var pages = getCurrentPages();//获取页面栈
                 var prePage = pages[pages.length - 2];
                 //调用上一个页面的onShow方法
                 prePage.onLoad();
                 wx.navigateBack({
                   delta: 1
                 });
                }, 2000);
              }
              return;
            }
          });
        }
      }
    });
  },

  agree: function(e) {
    var that = this;
    var type = e.currentTarget.dataset.type;
    if (type == 2) {
      that.setData({
        agree: true
      });
    } else {
      that.setData({
        agree: false
      });
    }
  },
  /* 点击减号 */
  bindMinus: function() {
    var num = this.data.num;
    // 如果大于1时，才可以减
    if (num > 1) {
      num--;
    }
    // 只有大于一件的时候，才能normal状态，否则disable状态
    var minusStatus = num <= 1 ? "disabled" : "normal";
    var vipspecPrice =(num*this.data.price).toFixed(2);
    // 将数值与状态写回
    this.setData({
      num: num,
      minusStatus: minusStatus,
      vipspecPrice:vipspecPrice
    });
  },
  /* 点击加号 */
  bindPlus: function() {
    var num = this.data.num;
    // 不作过多考虑自增1
    num++;
    // 只有大于一件的时候，才能normal状态，否则disable状态
    var minusStatus = num < 1 ? "disabled" : "normal";
    var vipspecPrice = (num*this.data.price).toFixed(2);
    // 将数值与状态写回
    this.setData({
      num: num,
      minusStatus: minusStatus,
      vipspecPrice:vipspecPrice
    });
  }
});
